package com.nanhai.buddhist.college.module.collegebase.controller.admin.teacherteachers;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;
import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.teacherteachers.vo.*;
import com.nanhai.buddhist.college.module.collegebase.convert.teacherteachers.TeacherTeachersConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.teacherteachers.TeacherTeachersDO;
import com.nanhai.buddhist.college.module.collegebase.service.teacherteachers.TeacherTeachersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 教师管理")
@RestController
@RequestMapping("/collegebase/teacher-teachers")
@Validated
public class TeacherTeachersController {

    @Resource
    private TeacherTeachersService teacherTeachersService;



    @PostMapping("/create")
    @ApiOperation("创建教师管理")
    @PreAuthorize("@ss.hasPermission('collegebase:teacher-teachers:create')")
    public CommonResult<String> createTeacherTeachers(@Valid @RequestBody TeacherTeachersCreateReqVO createReqVO) {
        return success(teacherTeachersService.createTeacherTeachers(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新教师管理")
    @PreAuthorize("@ss.hasPermission('collegebase:teacher-teachers:update')")
    public CommonResult<Boolean> updateTeacherTeachers(@Valid @RequestBody TeacherTeachersUpdateReqVO updateReqVO) {
        teacherTeachersService.updateTeacherTeachers(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除教师管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:teacher-teachers:delete')")
    public CommonResult<Boolean> deleteTeacherTeachers(@RequestParam("id") String id) {
        teacherTeachersService.deleteTeacherTeachers(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得教师管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:teacher-teachers:query')")
    public CommonResult<TeacherTeachersRespVO> getTeacherTeachers(@RequestParam("id") String id) {
        TeacherTeachersDO teacherTeachers = teacherTeachersService.getTeacherTeachers(id);
        return success(TeacherTeachersConvert.INSTANCE.convert(teacherTeachers));
    }

    @GetMapping("/list")
    @ApiOperation("获得教师管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('collegebase:teacher-teachers:query')")
    public CommonResult<List<TeacherTeachersRespVO>> getTeacherTeachersList(@RequestParam("ids") Collection<String> ids) {
        List<TeacherTeachersDO> list = teacherTeachersService.getTeacherTeachersList(ids);
        return success(TeacherTeachersConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得教师管理分页")
    public CommonResult<PageResult<TeacherTeachersRespVO>> getTeacherTeachersPage(@Valid TeacherTeachersPageReqVO pageVO) {
        PageResult<TeacherTeachersDO> pageResult = teacherTeachersService.getTeacherTeachersPage(pageVO);
        return success(TeacherTeachersConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出教师管理 Excel")
    @PreAuthorize("@ss.hasPermission('collegebase:teacher-teachers:export')")
    @OperateLog(type = EXPORT)
    public void exportTeacherTeachersExcel(@Valid TeacherTeachersPageReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        exportReqVO.setPageNo(1);
        exportReqVO.setPageSize(999999);
        PageResult<TeacherTeachersDO> pageResult= teacherTeachersService.getTeacherTeachersPage(exportReqVO);
       if(pageResult == null )pageResult = new PageResult<TeacherTeachersDO>();
        // 导出 Excel
        List<TeacherTeachersExcelVO> list = TeacherTeachersConvert.INSTANCE.convertList02(pageResult.getList());
        ExcelUtils.write(response, "教师管理.xls", "数据", TeacherTeachersExcelVO.class, list);
    }


    @GetMapping("/byFindStr/list")
    @ApiOperation("获得教师列表--可以根据名称模糊查询")
    @ApiImplicitParam(name = "findStr", value = "查询条件", required = true, example = "1024,2048", dataTypeClass = String.class)
    public CommonResult<List<TeacherTeachersRespVO>> getTeacherTeachersDOListByStr(String findStr) {
        List<TeacherTeachersDO> list = teacherTeachersService.getTeacherTeachersDOListByStr(findStr);
        return success(TeacherTeachersConvert.INSTANCE.convertList(list));
    }


    @GetMapping("/getByCollegeId")
    @ApiOperation("获得教师列表--根据学院信息")
    @ApiImplicitParam(name = "collegeId", value = "学院ID", required = true, example = "1024,2048", dataTypeClass = String.class)
    public CommonResult<List<TeacherInfoRespVO>> getByCollegeId(String collegeId) {
        List<TeacherTeachersDO> list = teacherTeachersService.getByCollegeId(collegeId);
        return success(TeacherTeachersConvert.INSTANCE.convertList04(list));
    }


}
